<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2022-03-26 01:16:12
 * @LastEditors: Sam
 * @LastEditTime: 2023-02-07 11:50:17
 */

declare (strict_types=1);

namespace app\common\traits;

use app\common\http\ResponseCode;
use think\response\Json;

/**
 * 返回响应Json
 * @create 2021年12月27日
 * @author maidou
 */
trait ResponseJson
{
    /**
     * 返回成功json
     *
     * @param null $message
     * @param null $data
     * @param int $code
     * @param array $header
     *
     * @return Json
     */
    protected function success(
        $msg = null,
        $data = null,
        $code = ResponseCode::SUCCESS,
        $header = []
    ) {
        return json([
            'code'  => $code,
            'msg'   => $msg,
            'data'  => $data,
        ]);
    }

    /**
     * 返回错误json
     *
     * @param null $message
     * @param int $code
     * @param array $data
     * @param array $header
     *
     * @return Json
     */
    protected function error($msg = null,$code = ResponseCode::ERROR,$data = [],$header = []) {
        return  json([
            'code'  => $code,
            'msg'   => $msg,
            'data'  => $data,
        ]);
    }

}
